package lol.clann.minecraft.springboot.api.bukkit.constant;

import com.ruoshui.utils.java.annotation.Nonnull;
import org.bukkit.block.BlockFace;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: zyp
 * @date: 2020/5/7 10:36 下午
 */
public enum BlockDirection {
    DOWN(0, -1, 0),
    UP(0, 1, 0),
    NORTH(0, 0, -1),
    SOUTH(0, 0, 1),
    WEST(-1, 0, 0),
    EAST(1, 0, 0);

    public final int offsetX;
    public final int offsetY;
    public final int offsetZ;
    public final int flag;
    public final BlockFace blockFace;
    private static final BlockDirection[] VALID_DIRECTIONS = new BlockDirection[]{DOWN, UP, NORTH, SOUTH, WEST, EAST};
    private static final BlockDirection[] OPPOSITES = new BlockDirection[]{UP, DOWN, SOUTH, NORTH, EAST, WEST};
    private static final Map<BlockFace, BlockDirection> map = new HashMap<>();

    static {
        for (BlockDirection value : BlockDirection.values()) {
            if (value.blockFace != null) {
                map.put(value.blockFace, value);
            }
        }
    }

    BlockDirection(int x, int y, int z) {
        this.offsetX = x;
        this.offsetY = y;
        this.offsetZ = z;
        this.flag = 1 << this.ordinal();
        if (this.ordinal() < 6) {
            this.blockFace = BlockFace.valueOf(this.name());
        } else {
            this.blockFace = null;
        }
    }

    @Nonnull
    public static BlockDirection from(BlockFace blockFace) {
        return map.get(blockFace);
    }

    public BlockFace getBlockFace() {
        return blockFace;
    }

    @Nonnull
    public static BlockDirection getByOrdinal(int id) {
        return id >= 0 && id < VALID_DIRECTIONS.length ? VALID_DIRECTIONS[id] : null;
    }

    /**
     * 获取对面方向
     *
     * @return
     */
    public BlockDirection getOpposite() {
        return OPPOSITES[this.ordinal()];
    }

    /**
     * 判断是否是对面
     *
     * @param direction
     * @return
     */
    public boolean isOpposite(BlockDirection direction) {
        return OPPOSITES[this.ordinal()] == direction;
    }
}
